package org.tron.common;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.tron.common.parameter.RateLimiterInitialization.createHttpItem;
import static org.tron.common.parameter.RateLimiterInitialization.createRpcItem;
import static org.tron.core.Constant.ECKey_ENGINE;

import com.google.common.collect.Lists;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigObject;
import java.util.ArrayList;
import java.util.HashSet;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.parameter.RateLimiterInitialization;
import org.tron.common.parameter.RateLimiterInitialization.HttpRateLimiterItem;
import org.tron.common.parameter.RateLimiterInitialization.RpcRateLimiterItem;
import org.tron.p2p.dns.update.PublishConfig;

public class ParameterTest {
  @Test
  public void testConstructor_ValidConfig() {
    String configStr = "{\"component\":\"testComponent\",\"strategy\":\"testStrategy\","
        + "\"paramString\":\"testParams\"}";
    ConfigObject config = ConfigFactory.parseString(configStr).root().toConfig().root();
    RpcRateLimiterItem item = new RpcRateLimiterItem(config);
    HttpRateLimiterItem item1 = new HttpRateLimiterItem(config);

    assertEquals("testComponent", item.getComponent());
    assertEquals("testComponent", item1.getComponent());
    assertEquals("testStrategy", item.getStrategy());
    assertEquals("testParams", item.getParams());
    assertNull(createRpcItem(null));
    assertNull(createHttpItem(null));
    assertNotNull(createRpcItem(config));
    RateLimiterInitialization rateLimiterInitialization = new RateLimiterInitialization();
    rateLimiterInitialization.setRpcMap(Lists.newArrayList(item));
    assertFalse(rateLimiterInitialization.isHttpFlag());
    assertTrue(rateLimiterInitialization.isRpcFlag());
  }

  @Test
  public void testCommonParameter() {
    CommonParameter parameter = new CommonParameter();
    parameter.setWitness(false);
    parameter.setSupportConstant(false);
    parameter.setMaxEnergyLimitForConstant(1000000L);
    parameter.setLruCacheSize(5);
    parameter.setMinTimeRatio(0);
    parameter.setMaxTimeRatio(20);
    parameter.setSaveInternalTx(false);
    parameter.setSaveFeaturedInternalTx(false);
    parameter.setLongRunningTime(60);
    parameter.setMaxHttpConnectNumber(5);

    assertEquals(StringUtils.EMPTY, parameter.getLogbackPath());
    assertEquals(1000000L, parameter.getMaxEnergyLimitForConstant());
    assertEquals(5, parameter.getLruCacheSize());
    assertEquals(60, parameter.getLongRunningTime());
    assertFalse(parameter.isHelp());
    assertFalse(parameter.isSaveFeaturedInternalTx());
    assertFalse(parameter.isSaveInternalTx());
    CollectionUtils.isEmpty(parameter.getSeedNodes());
    parameter.setChainId("123");
    assertEquals("123", parameter.getChainId());
    parameter.setNeedSyncCheck(false);
    assertFalse(parameter.isNeedSyncCheck());
    parameter.setNodeDiscoveryEnable(false);
    assertFalse(parameter.isNodeDiscoveryEnable());
    parameter.setNodeDiscoveryPersist(false);
    assertFalse(parameter.isNodeDiscoveryPersist());
    parameter.setNodeEffectiveCheckEnable(false);
    assertFalse(parameter.isNodeEffectiveCheckEnable());
    parameter.setNodeConnectionTimeout(500);
    assertEquals(500, parameter.getNodeConnectionTimeout());
    parameter.setFetchBlockTimeout(500);
    assertEquals(500, parameter.getFetchBlockTimeout());
    parameter.setNodeChannelReadTimeout(500);
    assertEquals(500, parameter.getNodeChannelReadTimeout());
    parameter.setMaxConnections(500);
    assertEquals(500, parameter.getMaxConnections());
    parameter.setMinConnections(500);
    assertEquals(500, parameter.getMinConnections());
    parameter.setMinActiveConnections(500);
    assertEquals(500, parameter.getMinActiveConnections());
    parameter.setMaxConnectionsWithSameIp(500);
    assertEquals(500, parameter.getMaxConnectionsWithSameIp());
    parameter.setMaxTps(500);
    assertEquals(500, parameter.getMaxTps());
    parameter.setMinParticipationRate(500);
    assertEquals(500, parameter.getMinParticipationRate());
    parameter.setMaxConnectionsWithSameIp(500);
    assertEquals(500, parameter.getMaxConnectionsWithSameIp());
    assertNull(parameter.getP2pConfig());
    parameter.setNodeLanIp("500");
    assertEquals("500", parameter.getNodeLanIp());
    parameter.setNodeP2pVersion(5);
    assertEquals(5, parameter.getNodeP2pVersion());
    parameter.setNodeEnableIpv6(false);
    assertFalse(parameter.isNodeEnableIpv6());
    parameter.setDnsTreeUrls(new ArrayList<>());
    assertTrue(CollectionUtils.isEmpty(parameter.getDnsTreeUrls()));
    parameter.setDnsPublishConfig(new PublishConfig());
    parameter.setSyncFetchBatchNum(500);
    assertEquals(500, parameter.getSyncFetchBatchNum());
    parameter.setDebug(false);
    assertFalse(parameter.isDebug());
    parameter.setFullNodeHttpPort(80);
    assertEquals(80, parameter.getFullNodeHttpPort());
    parameter.setSolidityHttpPort(80);
    assertEquals(80, parameter.getSolidityHttpPort());
    parameter.setJsonRpcHttpFullNodePort(80);
    assertEquals(80, parameter.getJsonRpcHttpFullNodePort());
    parameter.setJsonRpcHttpSolidityPort(80);
    assertEquals(80, parameter.getJsonRpcHttpSolidityPort());
    parameter.setJsonRpcHttpPBFTPort(80);
    assertEquals(80, parameter.getJsonRpcHttpPBFTPort());
    parameter.setRpcThreadNum(10);
    assertEquals(10, parameter.getRpcThreadNum());
    parameter.setSolidityThreads(5);
    assertEquals(5, parameter.getSolidityThreads());
    parameter.setMaxConcurrentCallsPerConnection(10);
    assertEquals(10, parameter.getMaxConcurrentCallsPerConnection());
    parameter.setFlowControlWindow(20);
    assertEquals(20, parameter.getFlowControlWindow());
    parameter.setMaxConnectionIdleInMillis(1000);
    assertEquals(1000, parameter.getMaxConnectionIdleInMillis());
    parameter.setBlockProducedTimeOut(500);
    assertEquals(500, parameter.getBlockProducedTimeOut());
    parameter.setNetMaxTrxPerSecond(15);
    assertEquals(15, parameter.getNetMaxTrxPerSecond());
    parameter.setMaxConnectionAgeInMillis(1500);
    assertEquals(1500, parameter.getMaxConnectionAgeInMillis());
    parameter.setMaxMessageSize(200);
    assertEquals(200, parameter.getMaxMessageSize());
    parameter.setMaxHeaderListSize(100);
    assertEquals(100, parameter.getMaxHeaderListSize());
    parameter.setRpcReflectionServiceEnable(false);
    assertFalse(parameter.isRpcReflectionServiceEnable);
    parameter.setValidateSignThreadNum(5);
    assertEquals(5, parameter.getValidateSignThreadNum());
    parameter.setMaintenanceTimeInterval(200);
    assertEquals(200, parameter.getMaintenanceTimeInterval());
    parameter.setProposalExpireTime(1000);
    assertEquals(1000, parameter.getProposalExpireTime());
    parameter.setAllowCreationOfContracts(1);
    assertEquals(1, parameter.getAllowCreationOfContracts());
    parameter.setAllowAdaptiveEnergy(1);
    assertEquals(1, parameter.getAllowAdaptiveEnergy());
    parameter.setAllowDelegateResource(1);
    assertEquals(1, parameter.getAllowDelegateResource());
    parameter.setAllowSameTokenName(1);
    assertEquals(1, parameter.getAllowSameTokenName());
    parameter.setAllowTvmTransferTrc10(1);
    assertEquals(1, parameter.getAllowTvmTransferTrc10());
    parameter.setAllowTvmConstantinople(1);
    assertEquals(1, parameter.getAllowTvmConstantinople());
    parameter.setAllowTvmSolidity059(1);
    assertEquals(1, parameter.getAllowTvmSolidity059());
    parameter.setForbidTransferToContract(1);
    assertEquals(1, parameter.getForbidTransferToContract());
    parameter.setTcpNettyWorkThreadNum(5);
    assertEquals(5, parameter.getTcpNettyWorkThreadNum());
    parameter.setUdpNettyWorkThreadNum(5);
    assertEquals(5, parameter.getUdpNettyWorkThreadNum());
    parameter.setTrustNodeAddr("address");
    assertEquals("address", parameter.getTrustNodeAddr());
    parameter.setWalletExtensionApi(false);
    assertFalse(parameter.isWalletExtensionApi());
    parameter.setEstimateEnergy(false);
    assertFalse(parameter.isEstimateEnergy());
    parameter.setEstimateEnergyMaxRetry(2);
    assertEquals(2, parameter.getEstimateEnergyMaxRetry());
    parameter.setKeepAliveInterval(1000);
    assertEquals(1000, parameter.getKeepAliveInterval());
    parameter.setReceiveTcpMinDataLength(10);
    assertEquals(10, parameter.getReceiveTcpMinDataLength());
    parameter.setOpenFullTcpDisconnect(false);
    assertFalse(parameter.isOpenFullTcpDisconnect());
    parameter.setNodeDetectEnable(false);
    assertFalse(parameter.isNodeDetectEnable());
    parameter.setAllowMultiSign(1);
    assertEquals(1, parameter.getAllowMultiSign());
    parameter.setVmTrace(false);
    assertFalse(parameter.isVmTrace());
    parameter.setNeedToUpdateAsset(false);
    assertFalse(parameter.isNeedToUpdateAsset());
    parameter.setTrxReferenceBlock("test");
    assertEquals("test", parameter.getTrxReferenceBlock());
    parameter.setTrxCacheEnable(false);
    assertFalse(parameter.isTrxCacheEnable());
    parameter.setAllowMarketTransaction(1);
    assertEquals(1, parameter.getAllowMarketTransaction());
    parameter.setAllowTransactionFeePool(1);
    assertEquals(1, parameter.getAllowTransactionFeePool());
    parameter.setAllowBlackHoleOptimization(1);
    assertEquals(1, parameter.getAllowBlackHoleOptimization());
    parameter.setAllowNewResourceModel(1);
    assertEquals(1, parameter.getAllowNewResourceModel());
    parameter.setEventSubscribe(false);
    assertFalse(parameter.isEventSubscribe());
    parameter.setTrxExpirationTimeInMilliseconds(100);
    assertEquals(100, parameter.getTrxExpirationTimeInMilliseconds());
    parameter.setAllowProtoFilterNum(10);
    assertEquals(10, parameter.getAllowProtoFilterNum());
    parameter.setShieldedTransInPendingMaxCounts(1);
    assertEquals(1, parameter.getShieldedTransInPendingMaxCounts());
    parameter.setChangedDelegation(1);
    assertEquals(1, parameter.getChangedDelegation());
    parameter.setActuatorSet(new HashSet<>());
    assertTrue(CollectionUtils.isEmpty(parameter.getActuatorSet()));
    parameter.setRateLimiterInitialization(new RateLimiterInitialization());
    assertNotNull(parameter.getRateLimiterInitialization());
    parameter.setRateLimiterGlobalQps(1000);
    assertEquals(1000, parameter.getRateLimiterGlobalQps());
    parameter.setRateLimiterGlobalIpQps(100);
    assertEquals(100, parameter.getRateLimiterGlobalIpQps());
    assertNull(parameter.getOverlay());
    assertNull(parameter.getEventPluginConfig());
    assertNull(parameter.getEventFilter());
    parameter.setCryptoEngine(ECKey_ENGINE);
    assertEquals(ECKey_ENGINE, parameter.getCryptoEngine());
    parameter.setFullNodeHttpEnable(false);
    assertFalse(parameter.isFullNodeHttpEnable());
    parameter.setSolidityNodeHttpEnable(false);
    assertFalse(parameter.isSolidityNodeHttpEnable());
    parameter.setMaxTransactionPendingSize(500);
    assertEquals(500, parameter.getMaxTransactionPendingSize());
    parameter.setPendingTransactionTimeout(500);
    assertEquals(500, parameter.getPendingTransactionTimeout());
    parameter.setNodeMetricsEnable(false);
    assertFalse(parameter.isNodeMetricsEnable());
    parameter.setMetricsStorageEnable(false);
    assertFalse(parameter.isMetricsStorageEnable());
    parameter.setInfluxDbIp("127.0.0.1");
    assertEquals("127.0.0.1", parameter.getInfluxDbIp());
    parameter.setInfluxDbPort(90);
    assertEquals(90, parameter.getInfluxDbPort());
    parameter.setInfluxDbDatabase("InfluxDb");
    assertEquals("InfluxDb", parameter.getInfluxDbDatabase());
    parameter.setMetricsReportInterval(100);
    assertEquals(100, parameter.getMetricsReportInterval());
    parameter.setMetricsPrometheusPort(3000);
    assertEquals(3000, parameter.getMetricsPrometheusPort());
    parameter.setAgreeNodeCount(10);
    assertEquals(10, parameter.getAgreeNodeCount());
    parameter.setAllowPBFT(1);
    assertEquals(1, parameter.getAllowPBFT());
    parameter.setPBFTHttpPort(70);
    assertEquals(70, parameter.getPBFTHttpPort());
    parameter.setPBFTExpireNum(100);
    assertEquals(100, parameter.getPBFTExpireNum());
    parameter.setAllowShieldedTRC20Transaction(10);
    assertEquals(10, parameter.getAllowShieldedTRC20Transaction());
    parameter.setAllowTvmIstanbul(1);
    assertEquals(1, parameter.getAllowTvmIstanbul());
    parameter.setAllowTvmVote(1);
    assertEquals(1, parameter.getAllowTvmVote());
    parameter.setAllowTvmLondon(1);
    assertEquals(1, parameter.getAllowTvmLondon());
    parameter.setAllowTvmCompatibleEvm(1);
    assertEquals(1, parameter.getAllowTvmCompatibleEvm());
    parameter.setAllowHigherLimitForMaxCpuTimeOfOneTx(1);
    assertEquals(1, parameter.getAllowHigherLimitForMaxCpuTimeOfOneTx());
    parameter.setHistoryBalanceLookup(false);
    assertFalse(parameter.isHistoryBalanceLookup());
    parameter.setOpenPrintLog(false);
    assertFalse(parameter.isOpenPrintLog());
    parameter.setOpenTransactionSort(false);
    assertFalse(parameter.isOpenTransactionSort());
    parameter.setAllowAssetOptimization(1);
    assertEquals(1, parameter.getAllowAssetOptimization());
    parameter.setAllowAccountAssetOptimization(1);
    assertEquals(1, parameter.getAllowAccountAssetOptimization());
    parameter.setBlockCacheTimeout(60);
    assertEquals(60, parameter.getBlockCacheTimeout());
    parameter.setAllowNewReward(1);
    assertEquals(1, parameter.getAllowNewReward());
    parameter.setAllowNewRewardAlgorithm(1);
    assertEquals(1, parameter.getAllowNewRewardAlgorithm());
    parameter.setMemoFee(100);
    assertEquals(100, parameter.getMemoFee());
    parameter.setAllowDelegateOptimization(1);
    assertEquals(1, parameter.getAllowDelegateOptimization());
    parameter.setUnfreezeDelayDays(10);
    assertEquals(10, parameter.getUnfreezeDelayDays());
    parameter.setAllowOptimizedReturnValueOfChainId(1);
    assertEquals(1, parameter.getAllowOptimizedReturnValueOfChainId());
    parameter.setAllowDynamicEnergy(1);
    assertEquals(1, parameter.getAllowDynamicEnergy());
    parameter.setDynamicEnergyThreshold(1);
    assertEquals(1, parameter.getDynamicEnergyThreshold());
    parameter.setDynamicEnergyIncreaseFactor(1);
    assertEquals(1, parameter.getDynamicEnergyIncreaseFactor());
    parameter.setDynamicEnergyMaxFactor(1);
    assertEquals(1, parameter.getDynamicEnergyMaxFactor());
    parameter.setDynamicConfigEnable(false);
    assertFalse(parameter.isDynamicConfigEnable());
    parameter.setDynamicConfigCheckInterval(10);
    assertEquals(10, parameter.getDynamicConfigCheckInterval());
    parameter.setAllowTvmShangHai(1);
    assertEquals(1, parameter.getAllowTvmShangHai());
    parameter.setAllowCancelAllUnfreezeV2(1);
    assertEquals(1, parameter.getAllowCancelAllUnfreezeV2());
    parameter.setMaxUnsolidifiedBlocks(100);
    assertEquals(100, parameter.getMaxUnsolidifiedBlocks());
    parameter.setAllowOldRewardOpt(1);
    assertEquals(1, parameter.getAllowOldRewardOpt());
    parameter.setAllowEnergyAdjustment(1);
    assertEquals(1, parameter.getAllowEnergyAdjustment());
    parameter.setMaxCreateAccountTxSize(1000);
    assertEquals(1000, parameter.getMaxCreateAccountTxSize());
  }
}
